package com.fly.caihong.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.fly.caihong.R;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.frag.HomeFragment;
import com.fly.caihong.frag.MessageFragment;
import com.fly.caihong.frag.MineFragment;
import com.fly.caihong.frag.VideoFragment;
import com.fly.caihong.utils.ActivityUtils;
import com.fly.caihong.utils.AppConfig;
import com.fly.caihong.utils.StatusBarUtils;
import com.fly.caihong.utils.ToastUtils;

import java.lang.reflect.Field;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView mBottomNavigation;
    private Fragment[] mTabFragments;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        super.initViews();
        assistBottomBar();
        initFragments();
    }

    private void assistBottomBar() {
        View rootView = findViewById(R.id.ll_root);
        StatusBarUtils.assistNavigationBar(this, rootView);
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        mBottomNavigation.setOnNavigationItemSelectedListener(this);
    }

    private void initFragments() {
        mBottomNavigation = findViewById(R.id.bottom_tab_layout);
        disableShiftMode(mBottomNavigation);
        mTabFragments = new Fragment[]{new HomeFragment()
                , new MessageFragment(), new VideoFragment(), new MineFragment()};
        FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : mTabFragments) {
            transition.add(R.id.home_container, fragment);
        }
        transition.commit();
    }

    @Override
    protected void initData() {
        super.initData();
        onTabItemSelected(R.id.tab_menu_home);
    }

    @SuppressLint("RestrictedApi")
    private void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }


    private void onTabItemSelected(int itemId) {
        switch (itemId) {
            case R.id.tab_menu_home:
                showTabFragment(mTabFragments[0]);
                break;
            case R.id.tab_menu_msg:
                showTabFragment(mTabFragments[1]);
                break;
            case R.id.tab_menu_video:
                showTabFragment(mTabFragments[2]);
                break;
            case R.id.tab_menu_mine:
                showTabFragment(mTabFragments[3]);
                break;
        }
    }

    private void showTabFragment(Fragment showFragment) {
        FragmentTransaction transition = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : mTabFragments) {
            if (fragment != showFragment) {
                transition.hide(fragment);
            } else {
                transition.show(showFragment);
            }
        }
        transition.commitAllowingStateLoss();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        onTabItemSelected(item.getItemId());
        return true;
    }

    @Override
    public void onBackPressed() {
        if (!AppConfig.isQuickClick2Times()) {
            ToastUtils.showLong(R.string.click_back_exit);
            return;
        }
        ActivityUtils.finishAllActivities(true);
    }

}
